<?php

class SmsController extends AppController {
    
    public $components = array('Api');
    
    public function beforeFilter() {
        parent::beforeFilter();
        
        $this->layout = "admin";
        
        // check if user is not authenticated and request login
        $this->validateOrganisationAccount();
        
        // get organisation account if we are still present here
        $orgAccount = $this->Session->read('org_account');
        
        // define account verification exclusion functions
        $actions = array('account_info');
        
        if( !in_array($this->params['action'], $actions) ) {
            $sms_account = $this->Api->post('/sms/account', array(
                'organisation_id' => $orgAccount['Organisation']['id']
            ));

            if( $sms_account['status'] == 'failed' ) {
                $this->autoRender = false;

                echo "SMS Functionality Not Enabled For This Organisation";
                return;
            }

            else {
                $this->set('sms_account', $sms_account['account']);
            }
        }
    }
    
    public function account_info() {
        $this->autoRender = false;
        
        // get organisation account if we are still present here
        $orgAccount = $this->Session->read('org_account');

        $sms_account = $this->Api->post('/sms/account', array(
            'organisation_id' => $orgAccount['Organisation']['id']
        ));
        
        return json_encode($sms_account);
    }
    
    public function home() {
        
        $this->redirect("/" . $this->params['organisationSlug'] . "/sms/instant");
    }
    
    public function members_list() {
        $this->autoRender = false;
        
        if( isset($_GET['term']) ) {
            $term = $_GET['term'];
            $orgAccount = $this->Session->read('org_account');
            
            $results = $this->Api->post('/sms/contacts/search', array(
                'organisation_id' => $orgAccount['Organisation']['id'],
                'term' => $term,
                'limit' => 5
            ));
            
            return json_encode($results);
        }
    }
    
    public function sendInstantMessage() {
        $this->autoRender = false;
        
        if( $this->request->is('post') ) {
            $data = $this->request->data;
            
            if( isset($data['contacts']) ) {
                $orgAccount = $this->Session->read('org_account');
                
                $response = $this->Api->post('/sms/send/instant', array(
                    'organisation_id' => $orgAccount['Organisation']['id'],
                    'contacts' => $data['contacts'],
                    'message' => $data['message']
                ));
                
                return json_encode($response);
            }
        }
    }
    
    public function sendBroadcastMessage() {
        $this->autoRender = false;
        
        if( $this->request->is('post') ) {
            $data = $this->request->data;
            
            $orgAccount = $this->Session->read('org_account');

            $params = array(
                'organisation_id' => $orgAccount['Organisation']['id'],
                'schedule' => $data['schedule'],
                'message' => $data['message']
            );
            
            if( isset($data['broadcast_list_id']) ) {
                $params['broadcast_list_id'] = $data['broadcast_list_id'];
            }
            
            if( isset($data['category_id']) ) {
                $params['category_id'] = $data['category_id'];
            }
            
            $response = $this->Api->post('/sms/send/broadcast', $params);

            return json_encode($response);
            
        }
    }
    
    public function instant() {
        //$orgAccount = $this->Session->read('org_account');
    }
    
    public function instant_history() {
        $orgAccount = $this->Session->read('org_account');
        
        $response = $this->Api->post('/sms/instant/history', array(
            'organisation_id' => $orgAccount['Organisation']['id'],
            'limit' => 10
        ));
        
        if( $response['status'] == 'success' ) {
            $this->set('history', $response['history']);
        }
    }
    
    public function broadcast() {
        $orgAccount = $this->Session->read('org_account');
        
        $response = $this->Api->post('/batch', array(
            'predefined' => array(
                'url' => '/sms/broadcast/predefined_lists',
                'method' => 'post',
                'params' => array(
                    'organisation_id' => $orgAccount['Organisation']['id']
                )
            ),
            'custom' => array(
                'url' => '/sms/broadcast/lists',
                'method' => 'post',
                'params' => array(
                    'organisation_id' => $orgAccount['Organisation']['id']
                )
            )
        ));
        
        $this->set('predefined_lists', $response['predefined']);
        
        if( $response['custom']['status'] == 'success' ) {
            $this->set('broadcast_lists', $response['custom']['lists']);
        }
    }
    
    public function broadcast_history() {
        $orgAccount = $this->Session->read('org_account');
        
        $response = $this->Api->post('/sms/broadcast/history', array(
            'organisation_id' => $orgAccount['Organisation']['id'],
            'limit' => 10
        ));
        
        if( $response['status'] == 'success' ) {
            $this->set('history', $response['history']);
        }
    }
    
    public function broadcast_lists() {
        $orgAccount = $this->Session->read('org_account');
        
        $response = $this->Api->post('/batch', array(
            'predefined' => array(
                'url' => '/sms/broadcast/predefined_lists',
                'method' => 'post',
                'params' => array(
                    'organisation_id' => $orgAccount['Organisation']['id']
                )
            ),
            'custom' => array(
                'url' => '/sms/broadcast/lists',
                'method' => 'post',
                'params' => array(
                    'organisation_id' => $orgAccount['Organisation']['id']
                )
            )
        ));
        
        $this->set('predefined_lists', $response['predefined']);
        
        if( $response['custom']['status'] == 'success' ) {
            $this->set('lists', $response['custom']['lists']);
        }
    }
    
    public function broadcast_list_contacts($list_type, $id) {
        $orgAccount = $this->Session->read('org_account');
        $response = null;
        
        if( $list_type == 'custom' ) {
            $response = $this->Api->post('/sms/broadcast_list/build', array(
                'organisation_id' => $orgAccount['Organisation']['id'],
                'broadcast_list_id' => $id,
                'limit' => 15
            ));
        }
        
        else if( $list_type == 'predefined' ) {
            $response = $this->Api->post('/sms/broadcast_list/predefined', array(
                'organisation_id' => $orgAccount['Organisation']['id'],
                'category_id' => $id,
                'limit' => 15
            ));
        }
        
        if( $response['status'] == 'success' ) {
            
            $this->set('list', $response['list']);
            $this->set('contacts', $response['contacts']);
        }
        
        else {
            
        }
    }
    
    public function broadcast_list_create($broadcast_list_id = null) {
        
        if( $this->request->is('post') ) {
            $this->autoRender = false;
            
            $data = $this->request->data;
            
            $response = $this->Api->post('/sms/broadcast_list/create', array(
                'data' => $data
            ));
            
            return json_encode($response);
        }
        
        $orgAccount = $this->Session->read('org_account');
        
        $sms_account = $this->Api->post('/sms/account', array(
            'organisation_id' => $orgAccount['Organisation']['id']
        ));
        
        if( $sms_account['status'] == 'success' ) {
            $this->set('account', $sms_account['account']);
        }
        
        $response = $this->Api->post('/batch', array(
            'categories' => array(
                'url' => '/organisation/categories',
                'method' => 'post',
                'params' => array(
                    'organisation_id' => $orgAccount['Organisation']['id']
                )
            ),
            'groups' => array(
                'url' => '/groups',
                'method' => 'post',
                'params' => array(
                    'organisation_id' => $orgAccount['Organisation']['id'],
                    'subgroups' => 1
                )
            )
        ));
        
        $this->set('categories', $response['categories']);
        $this->set('groups', $response['groups']);
        
        if( $broadcast_list_id != null ) {
            $list = $this->Api->post('/sms/broadcast_list', array(
                'organisation_id' => $orgAccount['Organisation']['id'],
                'broadcast_list_id' => $broadcast_list_id
            ));
            
            if( $list['status'] == 'success' ) {
                $this->set('broadcast_list', $list['broadcast_list']);
            }
        }
    }
    
    public function broadcast_list_delete() {
        $this->autoRender = false;
        
        if( $this->request->is('post') ) {
            $orgAccount = $this->Session->read('org_account');
            $data = $this->request->data;
            
            if( isset($data['broadcast_list_id']) ) {
                $response = $this->Api->post('/sms/broadcast_list/delete', array(
                    'organisation_id' => $orgAccount['Organisation']['id'],
                    'broadcast_list_id' => $data['broadcast_list_id']
                ));
                
                return json_encode($response);
            }
            
            return json_encode(array('status' => 'failed', 'message' => 'Required parameter \'broadcast_list_id\' not passed.'));
        }
    }
    
    public function broadcast_list_filter_delete() {
        $this->autoRender = false;
        
        if( $this->request->is('get') ) {
            $orgAccount = $this->Session->read('org_account');
            $data = $this->request->query;
            
            if( isset($data['filter_id']) ) {
                $response = $this->Api->post('/sms/broadcast_list/delete_filter', array(
                    'organisation_id' => $orgAccount['Organisation']['id'],
                    'filter_id' => $data['filter_id']
                ));
                
                return json_encode($response);
            }
            
            return json_encode(array('status' => 'failed', 'message' => 'Required parameter \'filter_id\' not passed.'));
        }
    }
    
    public function instant_message_popup() {
        $this->layout = "ajax";
        
        $orgAccount = $this->Session->read('org_account');
        
        if( $this->request->is('ajax') && $this->request->is('post') ) {
            $data = $this->request->data;
            
            $response = $this->Api->post('/sms/contacts/numbers', array(
                'organisation_id' => $orgAccount['Organisation']['id'],
                'numbers' => $data['numbers']
            ));
            
            if( $response['status'] == 'success' ) {
                $this->set('contacts', $response['contacts']);
            }
        }
        
    }
}

